// ExchangeDesk.cpp : implementation file
//

#include "stdafx.h"
#include "frontManage.h"
#include "ExchangeDesk.h"


// CExchangeDesk dialog

IMPLEMENT_DYNAMIC(CExchangeDesk, CDialog)

CExchangeDesk::CExchangeDesk(CWnd* pParent /*=NULL*/)
	: CDialog(CExchangeDesk::IDD, pParent)
	, m_comArea(_T(""))
	, m_comDesk(_T(""))
{
	m_selArea = 0;
}

CExchangeDesk::~CExchangeDesk()
{
}

void CExchangeDesk::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_AREA, m_areaBox);
	DDX_Text(pDX, IDC_COM_AREA, m_comArea);
	DDX_Text(pDX, IDC_COM_DESK, m_comDesk);
}


BEGIN_MESSAGE_MAP(CExchangeDesk, CDialog)
	ON_CBN_SELCHANGE(IDC_AREA, &CExchangeDesk::OnCbnSelchangeArea)
	ON_BN_CLICKED(IDOK, &CExchangeDesk::OnBnClickedOk)
END_MESSAGE_MAP()


// CExchangeDesk message handlers

void CExchangeDesk::OnCbnSelchangeArea()
{
	m_selArea = m_areaBox.GetCurSel();

	const vector<areaInfo>& vAreaInfo = theData->getAreaInfo();
	m_selArea = m_selArea % (int)vAreaInfo.size();
	m_DesktopDlg.setArea(vAreaInfo[m_selArea]);
}

BOOL CExchangeDesk::OnInitDialog()
{
	CDialog::OnInitDialog();

	m_DesktopDlg.Create(IDD_DESKTOP,this);
	m_DesktopDlg.ShowWindow(TRUE);
	m_DesktopDlg.SetWindowPos(this,5,50,440,385,SWP_NOZORDER);

	const vector<areaInfo>& vAreaInfo = theData->getAreaInfo();
	m_selArea = m_selArea % (int)vAreaInfo.size();
	m_DesktopDlg.setArea(vAreaInfo[m_selArea]);

	for (int i = 0; i < (int)vAreaInfo.size(); i++)
	{
		m_areaBox.AddString(vAreaInfo[i].areaName);
	}
	m_areaBox.SetCurSel(m_selArea);
	// TODO:  Add extra initialization here

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

LRESULT CExchangeDesk::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class
	if (message == WM_LBUTTON_HIT_DESKTOP)
	{
		theData->Lock();
		wchar_t* deskName = (wchar_t*)wParam;
		const vector<areaInfo>& vAreaInfo = theData->getAreaInfo();
		const vector<desktopInfo>& deskInfo = vAreaInfo[m_selArea].desktops;
		int nSelDesk = -1;
		for (size_t i = 0; i < deskInfo.size(); i++)
		{
			if(StrCmp(deskName,deskInfo[i].deskName) == 0)
				nSelDesk = (int)i;
		}
		theData->UnLock();
		if(nSelDesk >= 0)
			hitDesk(nSelDesk);
	}

	return CDialog::WindowProc(message, wParam, lParam);
}

void CExchangeDesk::hitDesk(int nSel)
{
	nHitArea = m_selArea;
	nHitDesk = nSel;

	const vector<areaInfo>& vAreaInfo = theData->getAreaInfo();
	if(vAreaInfo[nHitArea].desktops[nHitDesk].state != DESKSTATE_GREEN)
	{
		nHitArea = nHitDesk = -1;
		return;
	}
	m_comArea = vAreaInfo[nHitArea].areaName;
	m_comDesk = vAreaInfo[nHitArea].desktops[nHitDesk].deskName;
	UpdateData(FALSE);
}
void CExchangeDesk::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	if (nHitArea >= 0 && nHitDesk >= 0)
	{
		Order oldOrder;
		const vector<areaInfo>& vArea = theData->getAreaInfo();
		int nState = vArea[srcArea].desktops[srcDesk].state;
		if (nState == DESKSTATE_RED_ORDER)
		{
			if(!theData->getOrder(srcArea,srcDesk,&oldOrder))
				return;
			wcscpy_s(oldOrder.areaName,m_comArea);
			wcscpy_s(oldOrder.deskName,m_comDesk);

			netData sendOrder;
			sendOrder.type = dataProcessUpdateOrder;
			sendOrder.bufferLength = oldOrder.save(NULL);
			sendOrder.buffer = new char[sendOrder.bufferLength];
			oldOrder.save(sendOrder.buffer);
			processMain(&sendOrder,NULL);
			delete []sendOrder.buffer;

			theData->setOrder(nHitArea,nHitDesk,oldOrder);
		}

		setDeskState state;
		netData setState;
		state.areaName[0] = 0;
		state.deskName[0] = 0;
		wcscpy_s(state.areaName,vArea[srcArea].areaName);
		wcscpy_s(state.deskName,vArea[srcArea].desktops[srcDesk].deskName);
		state.nState = DESKSTATE_GREEN;
		setState.buffer = (char*)&state;
		setState.bufferLength = sizeof(state);
		setState.type = dataProcessSetState;

		processMain(&setState,NULL);

		state.areaName[0] = 0;
		state.deskName[0] = 0;
		wcscpy_s(state.areaName,m_comArea);
		wcscpy_s(state.deskName,m_comDesk);
		state.nState = nState;
		setState.buffer = (char*)&state;
		setState.bufferLength = sizeof(state);
		setState.type = dataProcessSetState;

		processMain(&setState,NULL);
	}
	OnOK();
}
